package com.ronri.ams.sample;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.ams.BaseFragment;
import com.ronri.ams.bean.regionbean;
import com.ronri.common.UrlEncode;
import com.ronri.db.RemoteDataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件：ams.java
 * 包名：com.ronri.ams.sample;
 * 说明：
 * 作者：MS During
 * 日期：2017/5/31
 * 历史：
 */
public class fragment_region extends BaseFragment {

    public interface IRegionCallBack{
        void CallBack(String szParentMb, String szParentCode);
    }

    public IRegionCallBack OnRegionCallBack = null;

    private Context mContext = null;

    // 列表控件
    private ListView mListView = null;

    // 数据源
    private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();

    // 远程服务器地址
    private String mRemoteAddress = null;

    // 关键字名
    private String mKeyName = "key";

    private String mJson = "";

    // 获取标题名称
    private String mKeyTitleName = "";

    // 选择的行信息
    private List<Integer> mSelectItems = new ArrayList<Integer>();

    // 远程数据
    private RemoteDataService mRemoteDB = new RemoteDataService();

    // 选择模式
    private int mSelectMode = ListView.CHOICE_MODE_SINGLE;

    private String mParentName = "";

    private String mRegion = "";

    private String mParentMb = "";

    public String getRegion() {
        return mRegion;
    }

    public void setRegion(String mRegion) {
        this.mRegion = mRegion;
    }

    public String getParentName() {
        return mParentName;
    }

    public void setParentName(String mParentName) {
        this.mParentName = mParentName;
    }

    public void BindCallback(IRegionCallBack callBack){
        OnRegionCallBack = callBack;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View returnView = inflater.inflate(R.layout.fragment_region, container, false);

        mContext = getContext();
        Log.i("aaa","fragment_region");
        // 获取列表
        mListView = (ListView)returnView.findViewById(R.id.listView);
        mListView.setChoiceMode(mSelectMode);

        mRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getRegion().getAddress());

        return returnView;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        getRemoteData();
    }

    // 返回父亲面包线
    public String getParentMb(){
        return mParentMb;
    }

    /***
     * 获取指定页面数据
     * @return
     */
    private int getRemoteData() {

        /**组合参数*/
        Map<String, Object> iTem = new HashMap<String, Object>();

        ///Bundle bundle = getArguments();
        //String szRegion = bundle.getString("region_code");

        Map<String, Object> mParam = new HashMap<>();

        mParam.put("addFrom", 1);
        mParam.put("addMark", 1);
        mParam.put("page", 1);
        mParam.put("pageSize", 100);
        mParam.put("region_code", mRegion);
        mParam.put("search", "");



        // 获取远程数据
        mRemoteDB.getRemoteData(mRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                "GET", new RemoteDataService.OnRemoteDataEvent() {
            /***
             * 返回结果
             *
             * @param szString
             */
            @Override
            public void OnResultEvent(String szString) {
                try {
                    regionbean r =  JSON.parseObject(szString, regionbean.class);

                    if(r.isSuccess()){

                        // mParentMb = r.getData().getRegion_name();
                        mItems.clear();

                        if (OnRegionCallBack != null){
                            OnRegionCallBack.CallBack(r.getData().getRegion_name(), r.getData().getRegion_code());
                        }

                        for (int i = 0; i < r.getData().getRows().size(); i++) {
                            Map<String, Object> iTemData = new HashMap<String, Object>();

                            iTemData.put("listTitle", r.getData().getRows().get(i).getName());
                            iTemData.put("listData", r.getData().getRows().get(i).getRegion_code());

                            mItems.add(iTemData);
                        }

                        ChoiceView adapter = new ChoiceView(mContext, mItems);
                        mListView.setAdapter(adapter);
                        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                String szData = mItems.get(i).get("listData").toString();
                                String szTitle = mItems.get(i).get("listTitle").toString();
                                ((activity_region_select)getActivity()).RegionSelected(szTitle, szData);
                            }
                        });
                    }
                } catch (JSONException ex) {

                }
            }

            /***
             * 进度展示
             *
             * @param nStatus
             */
            @Override
            public void OnRuningState(int nStatus) {
                // 显示进度条
            }

            /***
             * 回调获取参数
             *
             * @return
             */
            @Override
            public String OnGetCustomParam() {
                return null;
            }
        });

        return 0;
    }

}
